/**
 * Copyright(C) 2012-2014 shenzhen gcn99 Co., Ltd. All Rights Reserved.<br/>
 * 版权所有(C)2012-2014 <br/>
 * 公司名称：金中国黄金珠宝商城<br/>
 * 公司地址：广东省深圳市罗湖区文锦北路2028号逸湖居大厦三楼金中国<br/>
 * 网址：http://www.230la.net/com/3088909391/<br/>
 * <p>标        题：</p>
 * <p>文  件  名：com.goldcn.file.controller.FileController.java</p>
 * <p>部        门：研发一部
 * <p>版        本： 1.0</p>
 * <p>Compiler: JDK1.6.0_21</p>
 * <p>创  建  者：kylin.woo</p>
 * <p>创建时间：2016/03/04</p>
 * <p>修  改  者：</p>
 * <p>修改时间：</p>
 */

package com.goldcn.file.controller;


import com.goldcn.common.util.CommonUtils;
import com.goldcn.file.service.UploadService;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 文件上传处理类
 */
@RestController
public class FileController {

    @Autowired
    UploadService uploadService;

    private Logger logger = Logger.getLogger(FileController.class);

    private static final String Root = "upload-dir";

    /**
     * 文件下载
     *
     * @param fileName
     * @param response
     * @param measureType
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/file/{fileName:.+}")
    public Object downLoad(@PathVariable String fileName, HttpServletResponse response,
                           @RequestParam(value = "type", required = false) String measureType) {
        try {
            response.setContentType("text/html; charset=utf-8");
            if (CommonUtils.isNotEmpty(measureType)) {
                uploadService.downLoad(response, fileName, measureType);
            } else {
                uploadService.downLoad(response, fileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "{code:'success'}";
    }

    /**
     * 通用图片上传
     *
     * @param response
     * @param request
     * @param file
     * @param type     1,商品图片上传 0,其他图片上传
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/file", produces = "text/plain")
    @ResponseBody
    public Object uploadfile(HttpServletResponse response, HttpServletRequest request,
                             @RequestParam(value = "file", required = false) MultipartFile file,
                             @RequestParam(value = "type", required = false) String type) {
        return JSONObject.fromObject(uploadService.FileProcessing(file, type)).toString();
    }


    /**
     * 文件下载
     *
     * @param fileName
     * @param response
     * @param measureType
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/ftp/test")
    public Object doTest(@PathVariable String fileName, HttpServletResponse response,
                         @RequestParam(value = "type", required = false) String measureType) {

        return JSONObject.fromObject(uploadService.FileProcessing(null, null)).toString();

    }

}
